package com.pwt.repository;

import com.pwt.common.ResultPage;
import com.pwt.entity.Curves;
import com.pwt.entity.MachineManage;
import com.pwt.model.ComboBoxModel;
import com.pwt.model.CurvesHistoryModel;
import com.pwt.model.TreeComBoxModel;
import com.pwt.model.TreeModel;
import com.pwt.repository.base.DataRepositoryExtension;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dw
 * @date 2022/1/18 10:24
 * @Description
 */
public class CurvesRepositoryExtensionImpl extends DataRepositoryExtension<Curves,String> implements CurvesRepositoryExtension{

    @Override
    public List<Map<String, Object>> queryCurvesByCodeList(Integer k1000, Short k2000, Date k0004,List<String> k0014) {
        String sql = "SELECT \n" +
                "a.WV0014,\n" +
                "(SELECT PointData from Curves where a.WV0056=code) as PointData\n" +
                "from( \n" +
                "SELECT WV0014,WV0056 from WERTEVAR where WVTEIL =?0 and WVMERKMAL=?1  and WV0014 in(?2) \n" +
                ") a order by WV0014";
        //and WVDATZEIT<=?2
        return super.queryMapListBySql(sql,k1000,k2000,k0014);
    }


    public ResultPage<CurvesHistoryModel> queryCurvesHistory(Integer k0010, String k0014, Integer page, Integer limit){
        String sql ="SELECT \n" +
                "\tWV0014  as k0014,\n" +
                "\tWVDATZEIT as k0004 \n" +
                "FROM\n" +
                "\tWERTEVAR \n" +
                "WHERE\n" +
                "\tWVMASCHINE = ?0 and WV0014 is not null \n" +
                "\tAND WVDATZEIT <= ( SELECT MIN ( WVDATZEIT ) FROM WERTEVAR WHERE WV0014 = ?1 ) \n" +
                "GROUP BY\n" +
                "\tWV0014,\n" +
                "\tWVDATZEIT \n" +
                "ORDER BY\n" +
                "\tWVDATZEIT DESC;";
        String csql ="select count(1) from (SELECT\n" +
                "\tWV0014  as k0014,\n" +
                "\tWVDATZEIT as k0004 \n" +
                "FROM\n" +
                "\tWERTEVAR \n" +
                "WHERE\n" +
                "\tWVMASCHINE = ?0 and WV0014 is not null\n" +
                "\tAND WVDATZEIT <= ( SELECT MIN ( WVDATZEIT ) FROM WERTEVAR WHERE WV0014 = ?1 ) \n" +
                "GROUP BY\n" +
                "\tWV0014,\n" +
                "\tWVDATZEIT \n" +
                "\t) a ;";
        return super.queryCustomBeanPageBySql(CurvesHistoryModel.class,csql,sql,page-1,limit,k0010,k0014);
    }


    public List<TreeComBoxModel> queryK1000AndTime(String begin,String end,Integer k0010){
        String sql = "SELECT DISTINCT w.WVTEIL as id,t.TEBEZEICH name from WERTEVAR w left join  TEIL t on w.WVTEIL=t.TETEIL where w.WVMASCHINE = ?0  and WVDATZEIT >=?1 and WVDATZEIT <=?2 ";
        return super.queryCustomBeanListBySql(TreeComBoxModel.class,sql,k0010,begin,end);
    }

    public List<TreeComBoxModel> queryK0014AndTime(Integer k0010,Integer k1000,String begin,String end){
        String sql = "SELECT DISTINCT WV0014 id,WV0014 name from WERTEVAR  where WVMASCHINE =?0  and WVTEIL=?1 and WVDATZEIT >=?2 and WVDATZEIT <=?3  ";
        return super.queryCustomBeanListBySql(TreeComBoxModel.class,sql,k0010,k1000,begin,end);
    }
}
